<?php
// ----------------------------------------------------------------------
// pn-commerce Shopping Cart Solution for PostNuke
// http://pncommerce.de
// http://www.sourceforge.net/projects/pn-commerce/
//
// It is based on PostKart 0.701QA from
//                        Destin LeBlanc [ postkart@zenflesh.com ]
// ----------------------------------------------------------------------
// Source file:  $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pnimgsrc.php,v $
//
// Last changes from:  $Author: landseer $
// a never ending process
// Revision:           $Revision: 1.2 $
//
// Date of revision    $Date: 2004/01/12 10:13:58 $
//
// ----------------------------------------------------------------------
// LICENSE
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License (GPL)
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// To read the license please visit http://www.gnu.org/copyleft/gpl.html
// ----------------------------------------------------------------------

function smarty_function_pnimgsrc($params, &$smarty)
{
    // get the parameters
    extract($params); 
	unset($params['name']);
	unset($params['modname']);

    if (!isset($name)) {
        $smarty->trigger_error("pnimgsrc: attribute name required");
        return false;
    }
	
	// default for the module
    if (!isset($modname)) {
        $modname = $smarty->module;
    }    
	
	// always provide an alt attribute.
	// if none is set, assign an empty one.
    if (!isset($alt)) {
        $params['alt'] = '';
    }    

	// language 
	$lang =  pnVarPrepForOS(pnUserGetLang());

	// theme directory
    $theme         = pnVarPrepForOS(pnUserGetTheme());
    $osmodname     = pnVarPrepForOS($modname);
    $themelangpath = "themes/$theme/templates/modules/$osmodname/images/$lang";
    $themepath     = "themes/$theme/templates/modules/$osmodname/images";

	// module directory
    $modinfo       = pnModGetInfo(pnModGetIDFromName($modname));
	$osmoddir      = pnVarPrepForOS($modinfo['directory']);
    $modlangpath   = "modules/$osmoddir/pnimages/$lang";
    $modpath       = "modules/$osmoddir/pnimages";
    $syslangpath   = "system/$osmoddir/pnimages/$lang";
    $syspath       = "system/$osmoddir/pnimages";

    $ossrc = pnVarPrepForOS($name);

	// search for the image
    $imgsrc = '';
	foreach (array($themelangpath,
	               $themepath,
				   $modlangpath,
	               $modpath,
				   $syslangpath,
				   $syspath) as $path) {
        if (file_exists("$path/$ossrc") && is_readable("$path/$ossrc")) {
		    $imgsrc = "$path/$ossrc";
			break;
		}
    }

	if ($imgsrc == '') {
        $smarty->trigger_error("pnimgsrc: image $name not found");
        return;
	}

	return $imgsrc;
}

?>